# -- coding: utf-8 --

# 行政人员账号：WNXA088 Woniu.123456
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

def get_conn():
    import pymysql
    return pymysql.connect(host='192.168.44.128', user='root', password='123456', database='woniuboss4.0')

def update(sql):
    conn = get_conn()
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()
    cursor.close()
    conn.close()

def query(sql):
    conn = get_conn()
    cursor = conn.cursor()
    cursor.execute(sql)
    result = cursor.fetchall()
    cursor.close()
    conn.close()
    return result

if __name__ == '__main__':

    # 在自动化测试脚本中，不对登录功能进行完整的自动化测试
    # 面条式脚本：登录后进行添加设备以及查询设备这两个功能的UI测试脚本
    from selenium import webdriver
    driver = webdriver.Firefox()
    driver.implicitly_wait(5)
    driver.maximize_window()

    # 登录操作
    driver.get('http://192.168.44.128:8080/WoniuBoss4.0')
    driver.find_element(By.NAME, 'userName').send_keys('WNXA088')
    driver.find_element(By.NAME, 'userPass').send_keys('Woniu.123456')
    driver.find_element(By.CSS_SELECTOR, '.btn').click()

    # 数据库操作
    conn = get_conn()  # 数据库连接对象
    sql = 'SELECT count(assets_id) from assets'
    old = query(sql)  # 获得没有添加设置之前的记录数


    # 新增设备
    driver.get('http://192.168.44.128:8080/WoniuBoss4.0/assets')
    driver.find_element(By.CSS_SELECTOR, 'button.btn-padding:nth-child(1)').click()

    # 设备名称
    assets_name = driver.find_element(By.CSS_SELECTOR, '#addAss-form > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > select:nth-child(2)')
    asserts_size = len(Select(assets_name).options)
    import random
    random_index = random.randint(1, asserts_size-1)
    Select(assets_name).select_by_index(random_index)

    # 设备类型
    assets_type = driver.find_element(By.CSS_SELECTOR, 'select.form-control:nth-child(1)')
    type_size = len(Select(assets_type).options)
    random_index = random.randint(1, type_size - 1)
    Select(assets_type).select_by_index(random_index)

    # 条码
    barcode = driver.find_element(By.CSS_SELECTOR, '#addAss-form > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > input:nth-child(2)')
    barcode.send_keys('45234524526')

    price = driver.find_element(By.CSS_SELECTOR, '#addAss-form > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(2) > input:nth-child(2)')
    price.send_keys('100')

    employee = driver.find_element(By.XPATH, '/html/body/div[12]/div/div/form/div/div/div[3]/div[1]/input[1]')
    employee.send_keys('孔银花')
    import time
    time.sleep(2)
    driver.find_element(By.CSS_SELECTOR, '.dropdown-item').click()

    # 时间
    purchase_time = driver.find_element(By.CSS_SELECTOR, '#addAss-form > div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > input:nth-child(2)')
    purchase_time.click()
    driver.find_element(By.XPATH, '/html/body/div[9]/div[3]/table/tbody/tr[1]/td[3]').click()

    # 备注
    note = driver.find_element(By.CSS_SELECTOR, '#addAss-form > div:nth-child(1) > div:nth-child(1) > div:nth-child(4) > div:nth-child(1) > input:nth-child(2)')
    note.send_keys('说明')

    # 资产归属
    owner = driver.find_element(By.CSS_SELECTOR, '#addAss-form > div:nth-child(1) > div:nth-child(1) > div:nth-child(4) > div:nth-child(2) > select:nth-child(2)')
    owner_size = len(Select(owner).options)
    random_index = random.randint(1, owner_size-1)
    Select(owner).select_by_index(random_index)

    driver.find_element(By.CSS_SELECTOR, '#addAss > div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > button:nth-child(1)').click()

    time.sleep(2)

    # 获取实际结果
    new = query(sql)  # 获得添加设备之后的记录数
    if (new[0][0] - old[0][0]) == 1:
        actual = '添加成功'
    else:
        actual = '添加失败'

    # 断言：实际结果和期望结果的对比
    if '添加成功' == actual:
        print('断言成功')
    else:
        print('断言失败')



    assets_type = driver.find_element(By.NAME, 'type')
    Select(assets_type).select_by_visible_text('电脑')
    driver.find_element(By.CSS_SELECTOR, 'button.btn:nth-child(3)').click()

    # 获取实际结果的思路：1.获取页面上的记录总数；2.获取数据库表中按现有条件查询的记录总数；
    # 3.判断这两个记录总数是否一致；4.如果一致实际结果为“查询成功”，否则实际结果为“查询失败”
    # 4.使用期望结果和这个实际结果进行对比，如果通过证明测试成功，否则测试失败。

    result = driver.find_element(By.XPATH, '/html/body/div[8]/div[2]/div[3]/div[2]/div[4]/div[1]/span[1]').text
    import re
    query_count = re.findall('总共 (.*?) 条记录', result)[0]   # 获取页面记录的总数，是一个字符串
    sql = 'select count(*) from assets WHERE assets_name="01"'
    db_count = query(sql)

    if int(query_count) == db_count[0][0]:
        actual = '查询成功'
    else:
        actual = '查询失败'

    expected = '查询成功'
    if expected == actual:
        print('断言成功')
    else:
        print('断言失败')







